<template>
  <div class="history-container">
    <h2 class="history-title">聊天历史记录</h2>
    <el-empty v-if="!chatHistory.length" description="暂无历史记录" />
    <div v-else class="history-list">
      <el-collapse>
        <el-collapse-item v-for="(chat, index) in chatHistory" :key="index" :name="index">
          <template #title>
            <div class="history-item-header">
              <span class="history-time">{{ formatDate(chat.startTime) }}</span>
              <span class="history-preview">{{ getFirstMessage(chat.messages) }}</span>
            </div>
          </template>
          <div class="history-messages">
            <div v-for="(message, msgIndex) in chat.messages" :key="msgIndex" 
                 class="message-item" :class="{ 'message-self': message.isSelf }">
              <div v-if="!message.isSelf" class="avatar">
                <el-avatar :size="40" src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png" />
              </div>
              <div class="message-content">
                <div v-if="!message.isSelf" class="message-sender">{{ message.sender }}AI助手</div>
                <div class="message-content-wrapper">
                  <div class="message-text">{{ message.text }}</div>
                </div>
                <div class="message-time">{{ message.time }}</div>
              </div>
            </div>
          </div>
        </el-collapse-item>
      </el-collapse>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'

const chatHistory = ref([])

// 从本地存储加载历史记录
onMounted(() => {
  const savedHistory = localStorage.getItem('chatHistory')
  if (savedHistory) {
    chatHistory.value = JSON.parse(savedHistory)
  }
})

// 格式化日期
const formatDate = (timestamp) => {
  const date = new Date(timestamp)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 获取对话的第一条消息作为预览
const getFirstMessage = (messages) => {
  if (!messages || messages.length === 0) return '空对话'
  const firstUserMessage = messages.find(m => m.isSelf)
  return firstUserMessage ? firstUserMessage.text.slice(0, 30) + (firstUserMessage.text.length > 30 ? '...' : '') : '空对话'
}
</script>

<style scoped>
.history-container {
  padding: 20px;
  height: 100%;
  overflow-y: auto;
}

.history-title {
  margin-bottom: 20px;
  color: #333;
  font-size: 24px;
  font-weight: 500;
}

.history-list {
  max-width: 1000px;
  margin: 0 auto;
}

.history-item-header {
  display: flex;
  align-items: center;
  gap: 16px;
}

.history-time {
  color: #666;
  font-size: 14px;
  min-width: 150px;
}

.history-preview {
  color: #333;
  font-size: 14px;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-messages {
  padding: 16px;
}

.message-item {
  display: flex;
  margin-bottom: 20px;
  align-items: flex-start;
  gap: 8px;
}

.message-item.message-self {
  justify-content: flex-end;
}

.message-item:not(.message-self) {
  justify-content: flex-start;
}

.message-content {
  max-width: 60%;
}

.avatar {
  flex-shrink: 0;
}

.message-sender {
  font-size: 14px;
  color: #666;
  margin-bottom: 4px;
}

.message-content-wrapper {
  background-color: #f5f7fa;
  padding: 12px 16px;
  border-radius: 4px;
  margin-bottom: 4px;
}

.message-text {
  color: black;
  line-height: 1.5;
  white-space: pre-wrap;
}

.message-self .message-content-wrapper {
  background-color: #e6f7ff;
}

.message-time {
  font-size: 12px;
  color: #999;
  text-align: right;
}
</style>